package copy.anno.process;


import com.google.auto.service.AutoService;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Names;
import copy.application.Application;
import copy.code.GenerateClassCodeStrategy;
import copy.express.ParseNodeStackStrategy;
import copy.meta.impl.ParseEasycopyMetaDataStrategyHandler;
import copy.scan.ScanStrategy;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


@SupportedAnnotationTypes("copy.mark.EasyCopy")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@AutoService(Processor.class)
public class EasyCopyAnnotationProcessor extends AbstractProcessor {

    private JavacTrees javacTrees;
    private TreeMaker treeMaker;
    private Names names;
    //缓存转换器  key:全限定类名 value:转换器的body
    private Map<String,JCTree.JCBlock> transformMap = new HashMap<>();
    //上下文环境
    private Application application;
    private int count = 0;

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        if ( count == 0){
            JCTree.JCClassDecl jcClassDecl  = null;
            //roundEnv.getRootElements().add(jcClassDecl);
            //初始化Application必须属性
            Application.initSingleBean(processingEnv,roundEnv);
            //创建Application
            application = Application.getScanSingleBean();
            //初始化各个部分组件
            application.prepare();
            System.out.println("----------------------运行解析器:Scan,Meta,NodeStack-----------------------");
            try {
                //开启扫描器
                application.getStrategy(ScanStrategy.class).process();
                //开启元数据解析器
                application.getStrategy(ParseEasycopyMetaDataStrategyHandler.class).process();
                //开启NodeStack生成器,后缀表达式转换
                application.getStrategy( ParseNodeStackStrategy.class ).process();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            System.out.println("----------------------结束解析器:Scan,Meta,NodeStack-----------------------");
            //生成字节码:
            GenerateClassCodeStrategy generateClassCodeProcess = (GenerateClassCodeStrategy)application.getStrategy(GenerateClassCodeStrategy.class);
            try {
                generateClassCodeProcess.process();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            count++;
        }

        return true;
    }




    /**
     * 递归查找字段 a.b.c.d.e ,注意queue中不包含根
     *
     *  a.b.c.d.e
     *  根节点
     *  set或get
     *  set时使用的数据
     * @return
     */
    public String midExpressionToEndExpression(String ex){
        return ex;
    }


    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        //初始化上下文环境
        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();

        javacTrees = JavacTrees.instance(processingEnv);

        treeMaker = TreeMaker.instance(context);

        names = Names.instance(context);
    }

}
